package com.weilele.leisure.time.ui.fragment

import android.content.res.ColorStateList
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.weilele.leisure.time.R
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.view.BottomNavigateView

data class HomePageBean(
    var id: Long,
    var fgClass: Class<out Fragment>,
    var title: String?,
    var icon: Int,
    val enable: Function0<Boolean>
)

class HomePageAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
    val pages: MutableList<HomePageBean> = mutableListOf()
    override fun getItemCount(): Int = pages.count()

    override fun createFragment(position: Int): Fragment {
        return pages[position].fgClass.newInstance()
    }

    override fun getItemId(position: Int): Long {
        return pages[position].id
    }

    override fun containsItem(itemId: Long): Boolean {
        var containsItem = false
        pages.forEach {
            if (it.id == itemId) {
                containsItem = true
                return@forEach
            }
        }
        return containsItem
    }

}


class HomeMenuAdapter(val pages: MutableList<HomePageBean>) :
    BottomNavigateView.BottomNavigateAdapter<HomePageBean>(pages) {
    override fun getItemView(parent: ViewGroup, lp: ViewGroup.LayoutParams): View {
        return LayoutInflater.from(parent.context)
            .inflate(R.layout.item_home_bottom_bar, parent, false)
    }

    override fun onBindItemView(
        itemView: View,
        data: HomePageBean,
        position: Int,
        isSelect: Boolean
    ) {
        itemView.findViewById<TextView>(R.id.tvName)?.let {
            it.text = data.title
            if (isSelect) {
                it.setTextColor(getResColor(R.color.colorAccent))
            } else {
                it.setTextColor(getResColor(R.color.colorAppText))
            }
        }
        itemView.findViewById<ImageView>(R.id.icon)?.let {
            it.setImageResource(data.icon)
            if (isSelect) {
                it.imageTintList = ColorStateList.valueOf(getResColor(R.color.colorAccent))
            } else {
                it.imageTintList = ColorStateList.valueOf(getResColor(R.color.colorAppText))
            }
        }
    }
}

class HomePagesDiffCall(private val old :List<*>,private val new :List<*>):DiffUtil.Callback(){
    override fun getOldListSize(): Int {
        return old.count()
    }

    override fun getNewListSize(): Int {
        return new.count()
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return old[oldItemPosition] == new[newItemPosition]
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return old[oldItemPosition] == new[newItemPosition]
    }
}